package com.lanou.pianke.search.service.impl;


import com.lanou.pianke.search.EntityRepository.PieceRepository;
import com.lanou.pianke.search.entity.Piece;
import com.lanou.pianke.search.service.PieceService;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class PieceServiceImpl implements PieceService {

    @Autowired
    private PieceRepository pieceRepository;

    @Override
    public List<Piece> searchByPieceContent(String searchContent) {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
        builder.analyzer("ik").field("text");
        Iterable<Piece> search = pieceRepository.search(builder);
        Iterator<Piece> iterator = search.iterator();
        List<Piece> pieceList = new ArrayList<>();
        while (iterator.hasNext()){
            pieceList.add(iterator.next());
        }
        return pieceList;
    }
}
